package de.recipeminer.persistence;

import com.db4o.ObjectSet;

/**
 * Class to provide functionality for all classes with instances persisted into db4o.
 * Marks a classs to have non-primitive data members that should be
 * updated und activated recusively when instances of this annotation
 * are retrieved or stored in the Db4o.
 */
public abstract class Db4oPersistableObject <T extends  Db4oPersistableObject> {

    public ObjectSet<T> getAll() {
        Db4oContainer container = Db4oContainers.getDefault();
        return  container.query(thisClass());
    }


    /*public void save() {
        Db4oContainer container = Db4oContainers.getDefault();
        container.ext().isStored(this);

        //TODO: Logic error - produces duplicate instances in the database - object hast do be refetched in the
        // save session - maybe a different, more hollistic approach altogether should be found
        if (validate()) {
            ObjectContainer session = Db4oContainer.DEFAULT.startSession();
            session.store(this);
            session.close();
        } else {

        }
    }*/

    
    /*public boolean existsInDatabase()  {
        Db4oContainer  container = Db4oContainers.getDefault();

    }*/
    
    
    /**
     * Checks wether this object is currently valid and hast an internally
     * consitent state.
     * (Override this method in descendant classes to implement actual
     * validation)
     *
     * @return true, if this currently is valid
     */
    public boolean validate() {
        return true;
    }

    /** Require due to type erasure of generic type T; use this in implementing classes
     *  to provide the Class-object of the implementing class
     *
     * @return Class of the implementing class
     */
    protected abstract Class<T> thisClass();
}
